package com.example.peibei.ui.weight;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dingtao.common.bean.Choose;
import com.dingtao.common.bean.HomeCateTwo;
import com.dingtao.common.bean.ListResult;
import com.dingtao.common.bean.PayPower;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.exception.ApiException;
import com.example.peibei.OnCallback;
import com.example.peibei.R;
import com.example.peibei.service.presenter.ChooseListPresenter;
import com.example.peibei.service.presenter.ChooseServicePresenter;
import com.example.peibei.service.presenter.PayPowerPresenter;
import com.example.peibei.ui.activity.PersonInfoActivityTwo;
import com.example.peibei.ui.adapter.SelectRequestPaidanList;

import java.util.List;

public class SelectRequestPaidan extends AlertDialog.Builder {

    private ChooseListPresenter presenter;
    private SelectRequestPaidanList  adapter;
    private OnCallback onCallback;
    private String price;
    private AlertDialog dialog;

    public SelectRequestPaidan(Context context,String token,String id,String price) {
        super(context, R.style.Theme_dialog);

        PayPowerPresenter presenter2 = new PayPowerPresenter(new PayPowerData());
        presenter2.reqeust(token);
        presenter = new ChooseListPresenter(new ChooseListCall());
        presenter.reqeust(token,id);
        adapter = new SelectRequestPaidanList();
        adapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter a, @NonNull View view, int position) {
                Choose item = adapter.getItem(position);
                Bundle bundle = new Bundle();
                bundle.putString("mid", item.getMid());
                Intent it = new Intent(context,PersonInfoActivityTwo.class);
                it.putExtras(bundle);
                context.startActivity(it);
            }
        });
        this.price = price;
    }

    public void setOnCallback(OnCallback onCallback) {
        this.onCallback = onCallback;
    }

    @Override
    public AlertDialog create() {
        dialog = super.create();
        dialog.show();
        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width = getContext().getResources().getDisplayMetrics().widthPixels;
        window.setAttributes(attributes);
        dialog.setContentView(R.layout.dialog_select_request_paidan);
        RecyclerView rv = dialog.findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(getContext()));
        rv.setAdapter(adapter);
        TextView tv_price = dialog.findViewById(R.id.tv_price);
        tv_price.setText(price);
        int[] payType = {0};
        dialog.findViewById(R.id.layout_ali_pay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                payType[0] = 0;
                dialog.findViewById(R.id.ali_pay_iv).setVisibility(View.VISIBLE);
                dialog.findViewById(R.id.wechat_iv).setVisibility(View.GONE);
            }
        });
        dialog.findViewById(R.id.layout_wechat).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                payType[0] = 1;
                dialog.findViewById(R.id.ali_pay_iv).setVisibility(View.GONE);
                dialog.findViewById(R.id.wechat_iv).setVisibility(View.VISIBLE);
            }
        });
        dialog.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (adapter.getData().size() > 0){
                    dialog.dismiss();
                    int selectPosition = adapter.getSelectPosition();
                    Choose item = adapter.getItem(selectPosition);
                    if (onCallback != null){
                        onCallback.onCall(item,payType[0]);
                    }
                }
            }
        });
        return dialog;
    }

    class  ChooseListCall implements DataCall<ListResult<Choose>> {

        @Override
        public void success(ListResult<Choose> data, Object... args) {
            if (data.getRecords() != null && data.getRecords().size() > 0){
                data.getRecords().get(0).setSelect(true);
            }
            adapter.setNewInstance(data.getRecords());

        }

        @Override
        public void fail(ApiException e, Object... args) {
        }
    }


    public interface OnCallback{
        void  onCall(Choose item,int payType);
    }

    class  PayPowerData implements DataCall<List<PayPower>> {

        @Override
        public void success(List<PayPower> data, Object... args) {
            for (PayPower datum : data) {
                if (datum.getStatus() == 1){
                    if ("微信".equals(datum.getName())){
                        dialog.findViewById(R.id.layout_wechat).setVisibility(View.VISIBLE);
                    }else if ("支付宝".equals(datum.getName())){
                        dialog.findViewById(R.id.layout_ali_pay).setVisibility(View.VISIBLE);
                    }
                }
            }
        }

        @Override
        public void fail(ApiException data, Object... args) {

        }
    }
}
